<template>
  <div class="gear-container">
    <h2>CFMOTO 骑行装备</h2>
    <div class="product-grid">
      <div v-for="product in gearProducts" :key="product.id" class="product-card">
        <img :src="product.image" :alt="product.name">
        <h3>{{ product.name }}</h3>
        <p class="description">{{ product.description }}</p>
        <p class="price">¥{{ product.price }}</p>
        <div class="button-group">
          <button 
            @click="handleAddToCart(product)" 
            :disabled="!isLoggedIn"
            :title="!isLoggedIn ? '请先登录' : ''"
            class="cart-btn"
          >加入购物车</button>
          <router-link :to="`/product/${product.id}`" class="detail-btn">查看详情</router-link>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import { ref } from 'vue'
import { mapState, mapMutations } from 'vuex'

export default {
  name: 'Gear',
  setup() {
    const isLoggedIn = ref(localStorage.getItem('isLoggedIn') === 'true')
    return {
      isLoggedIn
    }
  },
  computed: {
    ...mapState(['products']),
    gearProducts() {
      return this.products.filter(product => product.productType === 'gear')
    }
  },
  methods: {
    ...mapMutations(['addToCart']),
    handleAddToCart(product) {
      if (!this.isLoggedIn) {
        alert('请先登录！')
        return
      }
      this.addToCart(product)
      alert('商品已添加到购物车！')
    }
  }
}
</script>

<style scoped>
.gear-container {
  padding: 20px;
}

.gear-container h2 {
  color: var(--cf-black);
  border-bottom: 2px solid var(--cf-orange);
  padding-bottom: 10px;
  margin-bottom: 30px;
  font-size: 1.8em;
  text-transform: uppercase;
  letter-spacing: 1px;
}

.product-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(300px, 1fr));
  gap: 30px;
  padding: 20px;
}

.product-card {
  border: 1px solid #ddd;
  border-radius: 8px;
  padding: 20px;
  text-align: center;
  transition: all 0.3s ease;
  background: white;
  box-shadow: 0 2px 5px rgba(0,0,0,0.1);
  position: relative;
  overflow: hidden;
}

.product-card::before {
  content: '';
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 4px;
  background: var(--cf-blue);
  transform: scaleX(0);
  transition: transform 0.3s ease;
}

.product-card:hover {
  transform: translateY(-5px);
  box-shadow: 0 5px 15px rgba(0,0,0,0.1);
}

.product-card:hover::before {
  transform: scaleX(1);
}

.product-card img {
  width: 100%;
  height: 200px;
  object-fit: cover;
  border-radius: 4px;
  margin-bottom: 15px;
}

.product-card h3 {
  margin: 15px 0;
  color: var(--cf-black);
  font-size: 1.2em;
  font-weight: 600;
}

.description {
  color: var(--cf-gray);
  margin: 10px 0;
  line-height: 1.5;
}

.price {
  font-size: 1.4em;
  color: var(--cf-orange);
  font-weight: bold;
  margin: 15px 0;
}

.button-group {
  display: flex;
  justify-content: center;
  gap: 10px;
  margin-top: 15px;
}

.cart-btn, .detail-btn {
  flex: 1;
  max-width: 120px;
  text-align: center;
  padding: 10px 15px;
  border-radius: 4px;
  font-weight: 500;
  transition: all 0.3s ease;
  text-decoration: none;
  display: inline-block;
}

.cart-btn {
  background-color: var(--cf-blue);
  color: var(--cf-white);
  border: none;
  cursor: pointer;
}

.cart-btn:disabled {
  background-color: #ccc !important;
  cursor: not-allowed;
  transform: none;
  opacity: 0.7;
}

.detail-btn {
  background-color: var(--cf-white);
  color: var(--cf-blue);
  border: 1px solid var(--cf-blue);
}

.cart-btn:hover:not(:disabled) {
  background-color: var(--cf-dark-blue);
  transform: translateY(-2px);
}

.detail-btn:hover {
  background-color: var(--cf-light-blue);
  transform: translateY(-2px);
}

@media (max-width: 768px) {
  .gear-container {
    padding: 15px;
  }

  .product-grid {
    gap: 15px;
    padding: 15px;
  }

  .product-card {
    padding: 15px;
  }
}
</style> 